package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

import javax.swing.JFrame;

import com.abigdreamer.im.server.log.EIMLogger;

public class MiniEdit extends JFrame implements ActionListener {
	 
	  MenuBar menuBar = new MenuBar();
	  Menu file = new Menu("文件"),
	       edit = new Menu("编辑"),
	       help = new Menu("帮助");
	  
	  MenuItem[] menuItem ={
	   new MenuItem("新建"),
	   new MenuItem("打开"),
	   new MenuItem("保存"),
	   new MenuItem("退出"),
	   new MenuItem("全选"),
	   new MenuItem("复制"),
	   new MenuItem("剪贴"),
	   new MenuItem("粘贴"),
	   new MenuItem("帮助")
	   };
	 
	  TextArea textArea = new TextArea();
	  String fileName = "NoName";
	  Toolkit toolKit = Toolkit.getDefaultToolkit();
	  Clipboard clipboard = toolKit.getSystemClipboard();
	  
	  //opne and close message dialogs
	  private FileDialog openFileDialog = 
	     new FileDialog(this,"Open File",FileDialog.LOAD);
	  private FileDialog saveFileDialog =
	     new FileDialog(this,"Save File",FileDialog.SAVE);
	     
	 public static void main(String[] args) {
	  // TODO: Add your code here
	  MiniEdit MyEdit = new MiniEdit();
	  MyEdit.setVisible(true);
	 }

	 /**
	  * Method MiniEdit
	  *
	  *
	  */
	 public MiniEdit() {
	  // TODO: Add your code here
	  setTitle("企业通讯记事本");
	  setFont(new Font("Times New Roman",Font.PLAIN,15));
	  setBackground(Color.white);
	  setSize(500,500);
	  setMenuBar(menuBar);
	  menuBar.add(file);
	  menuBar.add(edit);
	  menuBar.add(help);
	  for(int i=0;i<4;i++)
	  {
	   file.add(menuItem[i]);
	   edit.add(menuItem[i+4]);
	   }
	  help.add(menuItem[8]);
	     add(textArea);
	     
	  addWindowListener(new WindowAdapter(){
	     public void windowClosing(WindowEvent e){
	       e.getWindow().dispose();
	       setVisible(false);
	       }
	      });
	      
	     //add actionListener
	     for(int i=0;i<menuItem.length;i++)
	     {
	      menuItem[i].addActionListener(this);
	      }
	     
	 }


	 /**
	  * Method actionPerformed
	  *
	  *
	  * @param e
	  *
	  */
	 public void actionPerformed(ActionEvent e) {
	  Object eventSource = e.getSource();
	  
	  if(eventSource == menuItem[0])//newItem
	  {
	   textArea.setText("");
	   }
	 
	  else if(eventSource == menuItem[1])//OpenItem
	  {
	   openFileDialog.setVisible(true);
	   fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
	   if(fileName != null)
	   {
	    openFile(fileName);
	    }
	  }
	  
	  else if(eventSource ==menuItem[2])//SaveItem
	  {
	   saveFileDialog.setVisible(true);
	   fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
	   if(fileName !=null)
	   {
	    writeFile(fileName);
	    }
	   }
	  
	  else if(eventSource==menuItem[3])//exitItem
	  {
	   this.setVisible(false);
	   }
	  
	  else if(eventSource == menuItem[4])//Select All
	  {
	   textArea.selectAll();
	   }
	     else if(eventSource == menuItem[5])//copy
	     {
	      String text = textArea.getSelectedText();
	      StringSelection selection= new StringSelection(text);
	      
	      clipboard.setContents(selection,null);
	      
	      }
	     
	  else if(eventSource == menuItem[6])//cut
	  {
	   String text = textArea.getSelectedText();
	   StringSelection selection = new StringSelection(text);
	   
	   clipboard.setContents(selection,null);
	   textArea.replaceText("",textArea.getSelectionStart(),
	       textArea.getSelectionEnd());
	  }
	 
	    
	     else if(eventSource == menuItem[7])//Paste
	     {
	      Transferable contents = clipboard.getContents(this);
	      if(contents==null)
	      return;
	      String text;
	      text="";
	      try{
	       text = (String)contents.getTransferData(DataFlavor.stringFlavor);
	       
	       }catch(Exception ex){}
	      textArea.replaceText(text,
	         textArea.getSelectionStart(),textArea.getSelectionEnd());  
	      }
	     else if(eventSource == menuItem[8])
	     {
	     // JOptionPane.showMessageDialog(null,"This is a MiniEdit.");
	      } 
	  
	 }
	 
	 
	 //Read file
	 public void openFile(String fileName){
	   File file = new File(fileName);
	   openFile(file);
	  } 
	 
	 //Read file
	 public void openFile(File file){
	  try{
		  FileInputStream in=new FileInputStream(file);
		  InputStreamReader ins=new InputStreamReader(in,"UTF-8");
		   BufferedReader bufferread=new BufferedReader(ins);
		   String txt=bufferread.readLine();
		   StringBuffer str=new StringBuffer();
		   while(txt!=null){
			   str.append(txt+"\n");
			   txt=bufferread.readLine();
		   }
		   textArea.setText(str.toString());
		   }catch(Exception e)
		   {
		    EIMLogger.INFO("Error opening file!");
		    }
	  } 
	 
	 
	 //write file
	 public void writeFile(String fileName){
	  try{
	   File file = new File(fileName);
	   FileWriter write = new FileWriter(file);
	   write.write(textArea.getText());
	   write.close();
	   }catch(Exception e){
		   EIMLogger.INFO("Error closing file!");
	    }
	  }

	} 